package com.huarui.initialItem.dao.impl;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.ewaf.framework.dao.impl.BaseDaoImpl;
import org.springframework.orm.ibatis.SqlMapClientCallback;


import com.huarui.initialItem.dao.IinitialItemDao;
import com.huarui.initialItem.model.InitialItem;
import com.huarui.initialItem.util.InitialItemVO;
import com.ibatis.sqlmap.client.SqlMapExecutor;

public class InitialItemDaoImpl extends BaseDaoImpl implements IinitialItemDao {
	
	/**
	 * TODO : using ibatis iterate and startBatch
	 */
	public void batchInsert(final List<InitialItem> itemList) {
		this.getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
            public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {    
                executor.startBatch();
                int batch=0;
                for (InitialItem item:itemList) {    
                    executor.insert(namespace+".create",item);
                	batch++;
					if(batch==500){
						executor.executeBatch();
						batch=0;
					}
                }            
                executor.executeBatch();           
                return null;
            }
		});
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<InitialItemVO> pagingItemsByInitialId(Map map) {
		// TODO Auto-generated method stub
		return getSqlMapClientTemplate().queryForList(
				namespace + ".PagingItemsByInitialId", map);
	}

	@Override
	public void batchUpdate(final List<InitialItem> itemList) {
		this.getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
			@Override
			public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
				executor.startBatch();
				int batch=0;
				for(InitialItem item:itemList){
					executor.update(namespace+".update", item);
					batch++;
					if(batch==500){
						executor.executeBatch();
						batch=0;
					}
				}
				executor.executeBatch();
				return null;
			}
		});
	}
	
	public Integer deleteByCondition(Map<String,Object> map){
		return this.getSqlMapClientTemplate().delete(namespace+".deleteByCondition",map);
	}
	
	@Override
	public Integer pagingItemsCountByInitialId(Map<String,Object> map) {
		return (Integer) getSqlMapClientTemplate().queryForObject(namespace+".pagingItemsCountByInitialId",map);
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<InitialItem> findItemsByInitialId(Integer initialId) {
		return  (List<InitialItem>) this.getSqlMapClientTemplate().queryForObject(namespace+".findItemsByInitialId",initialId);
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<InitialItem> findAllItemsByCondition(Map<String,Object> map) {
		return this.getSqlMapClientTemplate().queryForList(namespace+".findAllItemsByCondition",map);
	}
	@Override
	public Integer findAllItemsCountByCondition(Map<String,Object> map) {
		return (Integer) this.getSqlMapClientTemplate().queryForObject(namespace+".findAllItemsCountByCondition",map);
	
	}
	@Override
	public Integer countErrors(Map<String,Object> map) {
		
		return (Integer) this.getSqlMapClientTemplate().queryForObject(namespace+".countErrors",map);
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<Map> MapItemsByCondition(Map<String,Object> map) {
		return this.getSqlMapClientTemplate().queryForList(namespace+".MapItemsByCondition",map);
	}

	@Override
	public Integer queryCodeCountByInitialId(Map<String,Object> map) {
		
		return (Integer) this.getSqlMapClientTemplate().queryForObject(namespace+".queryCodeCountByInitialId",map);
	}
	@Override
	public List<Map> queryCodeListByInitialId(Map<String,Object> map) {
		return this.getSqlMapClientTemplate().queryForList(namespace+".queryCodeListByInitialId",map);
	}
	
	
}
